package com.usian.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.mapper.ApArticleMapper;
import com.usian.model.article.dto.ArticleHomeDto;
import com.usian.model.article.dto.ArticleInfoDto;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.service.ApArticleConfigService;
import com.usian.service.ApArticleContentService;
import com.usian.service.ApArticleService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;
    @Value("${fileServerUrl}")
    private String fileServerUrl;

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @Autowired
    private ApArticleContentService apArticleContentService;

    @Override
    public ApArticle saveApArticle(ApArticle apArticle) {
        this.save(apArticle);
        return apArticle;
    }

    @Override
    public ResponseResult load(Short loadtype, ArticleHomeDto dto) {
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(dto.getSize()==null){
            dto.setSize(10); //默认值10条
        }

        //查询
        if(loadtype!=1 ||loadtype!=2){
            loadtype = 1; //默认查询更多
        }
        if(StringUtils.isBlank(dto.getTag())){
            dto.setTag("__all__");
        }
        if(dto.getMinBehotTime() == null) dto.setMinBehotTime(new Date());

        List<ApArticle> articleList = apArticleMapper.loadList(dto, loadtype);

        ResponseResult responseResult =ResponseResult.okResult(articleList);
        //涉及图片格式
        responseResult.setHost(fileServerUrl);
        return responseResult;
    }



    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        Map<String,Object> map =new HashMap<>();

        //查询配置信息
        ApArticleConfig articleConfig = apArticleConfigService.getOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));
        if(articleConfig==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        map.put("config",articleConfig);

        //查询文章内容
        ApArticleContent articleContent = apArticleContentService.getOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getArticleId()));
        if(articleContent==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        map.put("content",articleContent);
        return ResponseResult.okResult(map);
    }
}
